package io.renren.modules.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.Result;
import io.renren.common.exception.StatusCode;
import io.renren.modules.backstage.dao.ProductDao;
import io.renren.modules.backstage.entity.ProductEntity;
import io.renren.modules.backstage.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDao productDao;

    @Override
    public Result getAll(ProductEntity productEntity) {
        Page<ProductEntity> page = new Page<>(productEntity.getPageNum(),productEntity.getPageSize());
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        if(productEntity.getCompanyName()!= null && !productEntity.getCompanyName().equals("")){
            queryWrapper.like("company_name",productEntity.getCompanyName());
        }
        queryWrapper.eq("approval_status",1);
        Page<ProductEntity> entityPage = productDao.selectPage(page, queryWrapper);
        return new Result(StatusCode.OK,"贷款产品查询成功",true,entityPage);
    }

    @Override
    public ProductEntity getByProductId(Integer productId) {
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        return  productDao.selectOne(queryWrapper);
    }
}